package bean;

import com.demo.service.AuthenticationService;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 * User: yuebo
 * Date: 13-9-1
 * Time: 下午3:45
 */
@Component
@Scope("request")
public class LoginBean {
    private String username;
    private String password;

    @Autowired
    private AuthenticationService service;

    public void setService(AuthenticationService service) {
        this.service = service;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String login(){
        if(service.login(username,password)){
            return "/m/admin/home?faces-redirect=true";
        }else{
            FacesContext.getCurrentInstance().addMessage("error",new FacesMessage("User Name or Password is not correct, login denied."));
        }
        return null;
    }
}
